<?php
class Plogs {
	var $collection		= null;		
	var $cache			= null;
	var $clear			= null;	
	var $dbmongo		= null;
	/*
	------- table nlogs
	------- field
	* userid
	* type: {
				1:add cmt about place
				2:add image of place
			}
	* content: json {newid;title;posterid}
	* srcid
	* newid
	* isread: 0:no;1:yes
	* time
	*/
	public function __construct() {
		if(DB_MONGO_ENABLE) {
			$mongo 				= new Mongo();
			$this->dbmongo		= $mongo->selectDB('vfriend');
			$this->collection 	= $this->dbmongo->selectCollection('plogs');				
		}
	}
	public function delete() {
		if(DB_MONGO_ENABLE) {
			$this->collection->remove();
		}
	}
	public function deleteByTime($intUserId) {
		if(DB_MONGO_ENABLE) {
			$intTime = time() - LOGS_DELETE_TIME; 
			$this->collection->remove(array('time'=>'$lt ' . $intTime));
		}
	}
	public function deleteById($intId) {
		if(DB_MONGO_ENABLE) {
			$this->collection->remove(array('_id' => new MongoId($intId)), true);
		}
	}
	public function deleteByType($intType, $intSrcId) {
		if(DB_MONGO_ENABLE) {
			$this->collection->remove(array('type'=>$intType,'srcid'=>(int)$intSrcId));
		}
	}
	public function deleteByUserId($intUserId, $intPlaceId) {
		if(DB_MONGO_ENABLE) {
			$this->collection->remove(array('userid'=>$intUserId,'placeid'=>(int)$intPlaceId));
		}
	}
	public function insert($intUserId, $intType, $strContent, $intSrcId=0, $intPlaceId=0) {	
		try {
			if(DB_MONGO_ENABLE) {
				$objInsert = array(	'userid'		=> (int)$intUserId, 									
									'type' 			=> (int)$intType,
									'content'		=> (string)$strContent,
									'placeid'		=> (int)$intPlaceId,
									'srcid'			=> (int)$intSrcId,
									'isread'		=> 0,
									'time'			=> time()
							);		
				$this->collection->insert($objInsert);						
			}
		} catch(Exception $e) {
			Vfriend_Function::logDb('Plogs::insert - ' .$e->getMessage());			
			return null;
		}
	}
	
	public function updateRead($intUserId, $intPlaceId) {
		try {		
			if(DB_MONGO_ENABLE) {
				$objWhere 	= array('isread'=>0,'userid'=>(int)$intUserId, 'placeid'=>(int)$intPlaceId);
				$objUpdate	= array('isread'=>1);
				$objMulti	= array('multiple'=>true);
				$this->collection->update($objWhere, array('$set'=>$objUpdate),$objMulti);				
			}
		} catch(Exception $e) {
			Vfriend_Function::logDb('Plogs::updateRead - ' .$e->getMessage());			
			return null;
		}
	}
	public function getPlogsByUserId($intUserId, $intOffset, $intLimit, &$intTotal=0) {
		try {			
			if(DB_MONGO_ENABLE) {						
				$objFind 	= array('userid'=>(int)$intUserId);
				$objSort	= array('time'=>-1);
				$cursor 	= $this->collection->find($objFind);
				$intTotal	= $cursor->count();
				$arrData	= $cursor->sort($objSort)->limit((int)$intLimit)->skip((int)$intOffset);
				if((object)$arrData) {
					$arrResult = null;
					foreach($arrData as $value) {
						$intPlaceId =	$value['placeid'];
						if((isset($arrResult) && !array_key_exists($intPlaceId,$arrResult)) || !isset($arrResult)) {
							$value['postdate'] 		= date('d/m/Y H:i:s',$value['time']);
							$value['content']		= Zend_Json::decode($value['content']);
							$arrResult[$intPlaceId] 	= $value;
						}
					}
					return $arrResult;
				}
			}
			return null;
		} catch(Exception $e) {
			Vfriend_Function::logDb('Plogs::getPlogsByUserId - ' .$e->getMessage());			
			return null;
		}
	}
}
?>
